#include<bits/stdc++.h>
using namespace std;
#define N 5050
typedef long long LL;
LL p[N];
LL n, k;
LL c[N];
int v[N<<1];
void init(){
    scanf("%lld%lld", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%lld", &p[i]);
    for(int i = 1; i <= n; i++) scanf("%lld", &c[i]);
    return ;
}
/*
3 3
2 3 1
-1 3 4
*/
LL r[N<<1];
LL s[N<<1], q[N<<1];
void solve(){
    LL ans = LONG_LONG_MIN;
    for(int i = 1; i <= n; i++){
        if(v[i]) continue;
        r[0] = 0;
        r[++r[0]] = c[i];
        v[i] = 1;
        for(int j = p[i]; !v[j]; j = p[j]) r[++r[0]] = c[j], v[j] = 1;
        LL x = k % r[0], y = k / r[0];
        s[0] = 0;
        for(int j = 1; j <= r[0]; j++) s[j] = s[j - 1] + r[j];
        LL sum = s[r[0]] * y;
        for(int j = r[0] + 1; j <= r[0] * 2; j++) s[j] = s[r[0]] + s[j - r[0]];
        
        if(y == 1 && x == 0) x = r[0], sum = LONG_LONG_MIN / 2;
        else if(sum < 0LL) sum = 0LL, x = min(max(x, (LL)k), r[0] - 1);
        int m=r[0] + x - 1;
        int head=0,tail=0;
        LL p=LONG_LONG_MIN / 2;
        if(x > 0){
            for(int j=1;j<=m;j++)
            {
                while(head<tail&&s[j-1]<s[q[tail-1]])
                    tail--;
                q[tail++]=j-1;
                while(head<tail&&j-q[head]>x) head++;
                if(p<s[j]-s[q[head]])
                {
                    p=s[j]-s[q[head]];
                }            
            }
        }
        
        if(p != LONG_LONG_MIN) sum = max(p, sum + p);
        if(sum > ans) ans = sum;
    }
    printf("%lld\n", ans);
    return ;
}
int main(){
    init();
    solve();
    return 0;
}